package com.bto.solar.work.config;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.bto.solar.work.dto.order.DesignDto;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 王小波
 * @description:
 * @date 2023/11/1 14:41
 */

@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MybatisOrderDesignDeviceTypeHandler extends BaseTypeHandler<List<DesignDto.DesignDevice>> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<DesignDto.DesignDevice> designDevices, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(designDevices));
    }

    @Override
    public List<DesignDto.DesignDevice> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return JSONArray.parseArray(resultSet.getString(s) , DesignDto.DesignDevice.class);
    }

    @Override
    public List<DesignDto.DesignDevice> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return JSONArray.parseArray(resultSet.getString(i) , DesignDto.DesignDevice.class);
    }

    @Override
    public List<DesignDto.DesignDevice> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return JSONArray.parseArray(callableStatement.getString(i) , DesignDto.DesignDevice.class);
    }
}
